// Decompiled by DJ v3.9.9.91 Copyright 2005 Atanas Neshkov  Date: 2008-6-29 21:33:59
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   BaseAction.java

package com.creawor.imei.base;

import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.*;

import org.apache.struts.action.*;

public class BaseAction extends Action
{

    public BaseAction()
    {
        pagesize = new Integer(15);
        pageno = new Integer(1);
        totalpage = new Integer(0);
        action = null;
        sql_string = null;
        sql_param = null;
        sql_param_str = null;
    }

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws Exception
    {
        java.util.Locale locale = getLocale(request);
        org.apache.struts.util.MessageResources messages = getResources(request);
        action = request.getParameter("act");
        sql_string = request.getParameter("sql_string");
        sql_param_str = request.getParameter("sql_param");
        sql_param = strToMap(sql_param_str);
        if(isCancelled(request))
            action = doCancel(mapping, form, request, response);
        else
        if(action == null)
        {
            request.getSession().setAttribute("pageno", null);
            action = doList(mapping, form, request, response);
        } else
        if(action.equals("list"))
        {
            if(request.getParameter("table") != null || request.getParameter("order") != null)
            		if(null==request.getParameter("multisql"))
            			getSQL(request);
            		else
            			getMULTISQL(request);
            action = doList(mapping, form, request, response);
        } else
        if(action.equals("create"))
            action = doCreate(mapping, form, request, response);
        else
        if(action.equals("add"))
            action = doAdd(mapping, form, request, response);
        else
        if(action.equals("edit"))
            action = doEdit(mapping, form, request, response);
        else
        if(action.equals("update"))
            action = doUpdate(mapping, form, request, response);
        else
        if(action.equals("delete"))
            action = doDelete(mapping, form, request, response);
        else
        if(action.equals("detail"))
            action = doDetail(mapping, form, request, response);
        else
            if(action.equals("detailView"))
                action = doDetailView(mapping, form, request, response);
        else
        if(action.equals("deleteall"))
            action = doDeleteAll(mapping, form, request, response);
        else
        if(action.equals("searchpage"))
            action = doQuery(mapping, form, request, response);
        else
        if(action.equals("search"))
            action = doList(mapping, form, request, response);
        else if(action.equals("delattach"))
            action = doDelattach(mapping, form, request, response);
        else
            action = doOther(mapping, form, action, request, response);
        return mapping.findForward(action);
    }

    private String doDetailView(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		return "detailView";
	}

	public String doList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws Exception
    {
        return "list";
    }

    public String doDetail(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws Exception
    {
        return "detail";
    }

    public String doCreate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        return "addpage";
    }

    public String doAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws Exception
    {
        return "list";
    }

    public String doEdit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws Exception
    {
        return "editpage";
    }

    public String doUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws Exception
    {
        return "aftersave";
    }

    public String doDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws Exception
    {
        return "aftersave";
    }

    public String doDeleteAll(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws Exception
    {
        return "aftersave";
    }

    public String doQuery(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws Exception
    {
        return "searchpage";
    }

    public String doCancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws Exception
    {
        String a = doList(mapping, form, request, response);
        return a;
    }
    public String doDelattach(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception
	{
	   // String a = doList(mapping, form, request, response);
	    return "editpage";
	}

    public String doOther(ActionMapping mapping, ActionForm form, String action, HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
        throws Exception
    {
        return action;
    }

    public String Date2String(Object date)
    {
        if(date == null)
        {
            return "";
        } else
        {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            return dateFormat.format(date);
        }
    }

    public void getMULTISQL(HttpServletRequest request)
        throws SQLException
    {
    	
    	StringBuffer sql = new StringBuffer("FROM ");
      sql.append(request.getParameter("table"));
      String sqlstr=request.getParameter("sql_string");
      sql.append(" where ").append(sqlstr);
      this.sql_string=sql.toString();
    }

    public void getSQL(HttpServletRequest request)
    throws SQLException
{
    Enumeration enum1 = request.getParameterNames();
    HashMap hmap = new HashMap();
    while(enum1.hasMoreElements()) 
    {
        String field = (String)enum1.nextElement();
        String value = request.getParameter(field);
        String field_op = request.getParameter((new StringBuilder(String.valueOf(field))).append("_op").toString());
        if(value != null && field_op != null && !value.equals("") && !field_op.equals(""))
        {
            if(field_op.equalsIgnoreCase("LIKE"))
                value = (new StringBuilder("%")).append(value).append("%").toString();
            hmap.put(field, value);
        }
    }
    StringBuffer sql = new StringBuffer("FROM ");
    sql.append(request.getParameter("table"));
    if(hmap.size() > 0)
    {
        Iterator iterator = hmap.keySet().iterator();
        for(int i = 0; iterator.hasNext(); i++)
        {
            if(i == 0)
                sql.append(" WHERE ");
            else
                sql.append(" AND ");
            String field = (String)iterator.next();
            String value = (String)hmap.get(field);
            String op = request.getParameter((new StringBuilder(String.valueOf(field))).append("_op").toString());
            String dflag = field.substring(field.length() - 2, field.length());
            if(dflag.equals("_1") || dflag.equals("_2"))
                field = field.substring(0, field.length() - 2);
            sql.append(field);
            if(op.equals("="))
            {
                sql.append(" = :");
                sql.append(field);
            } else
            if(op.equals("!="))
            {
                sql.append(" != :");
                sql.append(field);
            } else
            if(op.equals(">"))
            {
                sql.append(" > :");
                sql.append(field);
            } else
            if(op.equals("<"))
            {
                sql.append(" < :");
                sql.append(field);
            } else
            if(op.equals(">="))
            {
                sql.append(" >= :");
                sql.append(field);
            } else
            if(op.equals("<="))
            {
                sql.append(" <= :");
                sql.append(field);
            } else
            if(op.equalsIgnoreCase("LIKE"))
            {
                sql.append(" LIKE :");
                sql.append(field);
            }
        }

    }
    String order = request.getParameter("order");
    String by = request.getParameter("by");
    if(order != null && by != null && !order.equals("") && !by.equals(""))
    {
        sql.append(" ORDER BY ");
        sql.append(order);
        sql.append((new StringBuilder(" ")).append(by).toString());
    }
    String order_1 = request.getParameter("order_1");
    String by_1 = request.getParameter("by_1");
    if(order_1 != null && by_1 != null && !order_1.equals("") && !by_1.equals("") && !order_1.equals(order))
        if(order != null && by != null && !order.equals("") && !by.equals(""))
        {
            sql.append((new StringBuilder(",")).append(order_1).toString());
            sql.append((new StringBuilder(" ")).append(by_1).toString());
        } else
        {
            sql.append(" ORDER BY ");
            sql.append(order_1);
            sql.append((new StringBuilder(" ")).append(by_1).toString());
        }
    sql_string = sql.toString();
    sql_param = hmap;
    sql_param_str = mapToStr(hmap);
}
    public HashMap strToMap(String str)
    {
        HashMap hashmap;
label0:
        {
            String strArray[] = (String[])null;
            String strArray2[][] = (String[][])null;
            hashmap = new HashMap();
            if(str != null && !str.equals(""))
            {
                getClass();
                if(str.indexOf(",") != -1)
                    break label0;
            }
            return null;
        }
        getClass();
        if(str.indexOf(";") >= 0)
        {
            getClass();
            String strArray[] = str.split(";");
            for(int i = 0; i < strArray.length; i++)
            {
                getClass();
                String a[] = strArray[i].split(",");
                hashmap.put(a[0], a[1]);
            }

        } else
        {
            String strArray[] = str.split(",");
            hashmap.put(strArray[0], strArray[1]);
        }
        return hashmap;
    }

    public String mapToStr(HashMap hashmap)
    {
        if(hashmap == null || hashmap.size() == 0)
            return null;
        StringBuffer str = new StringBuffer();
        Iterator iterator = hashmap.keySet().iterator();
        for(int i = 0; iterator.hasNext(); i++)
        {
            if(i > 0)
            {
                getClass();
                str.append(";");
            }
            String key = (String)iterator.next();
            str.append(key);
            getClass();
            str.append(",");
            str.append((String)hashmap.get(key));
        }

        return str.toString();
    }

    public Integer pagesize;
    public Integer pageno;
    public Integer totalpage;
    private String action;
    public String sql_string;
    public HashMap sql_param;
    public final String SEPARATOR = ";";
    public final String LINKER = ",";
    public String sql_param_str;
}